package xcmg.device.service.question;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.question.QuestionTrackDO;
import xcmg.device.dao.mapper.question.QuestionTrackMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.question.QuestionTrackVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class QuestionTrackService {

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private QuestionTrackMapper questionTrackMapper;

    public ErrorCode add(QuestionTrackVO questionTrackVO, String userId, String userName) throws Exception {
        QuestionTrackDO questionTrackDO = new QuestionTrackDO();
        BeanUtils.copyProperties(questionTrackVO, questionTrackDO);
        questionTrackDO.setId(getId());
        Date currentTime = new Date();
        questionTrackDO.setCreateId(userId);
        questionTrackDO.setCreateName(userName);
        questionTrackDO.setCreateTime(currentTime);
        questionTrackDO.setUpdateId(userId);
        questionTrackDO.setUpdateName(userName);
        questionTrackDO.setUpdateTime(currentTime);
        questionTrackMapper.insert(questionTrackDO);
        return ErrorCode.Success;
    }

    public ErrorCode update(QuestionTrackVO questionTrackVO, String userId, String userName) {
        QuestionTrackDO questionTrackDO = new QuestionTrackDO();
        BeanUtils.copyProperties(questionTrackVO, questionTrackDO);
        Date currentTime = new Date();
        questionTrackDO.setUpdateId(userId);
        questionTrackDO.setUpdateName(userName);
        questionTrackDO.setUpdateTime(currentTime);
        questionTrackMapper.update(questionTrackDO);
        return ErrorCode.Success;
    }

    public QuestionTrackVO item(String id) throws Exception {
        QuestionTrackDO questionTrackDO = questionTrackMapper.item(id);
        if (questionTrackDO == null) {
            throw new Exception("单据不存在");
        }
        QuestionTrackVO questionTrackVO = new QuestionTrackVO();
        BeanUtils.copyProperties(questionTrackDO, questionTrackVO);
        return questionTrackVO;
    }

    public PageCommonVO pageList(SearchCommonVO<QuestionTrackVO> condition) {
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<QuestionTrackDO> questionTrackDOs = questionTrackMapper.list(condition.getFilters());
        PageCommonVO pageCommonVO = new PageCommonVO();
        pageCommonVO.setPageInfo(new PageInfo(questionTrackDOs));
        List<QuestionTrackVO> questionTrackVOs = new ArrayList<QuestionTrackVO>();
        for (QuestionTrackDO questionTrackDO : questionTrackDOs) {
            QuestionTrackVO questionTrackVO = new QuestionTrackVO();
            BeanUtils.copyProperties(questionTrackDO, questionTrackVO);
            questionTrackVOs.add(questionTrackVO);
        }
        pageCommonVO.setPageInfoList(questionTrackVOs);
        return pageCommonVO;
    }

    public ErrorCode delete(String id) {
        questionTrackMapper.delete(id);
        return ErrorCode.Success;
    }

    public ErrorCode batchDel(List<String> ids) {
        for (String id : ids) {
            questionTrackMapper.delete(id);
        }
        return ErrorCode.Success;
    }

    private String getId() throws Exception {
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult == null || textResult.getCode() != 0) {
            throw new Exception("生成ID失败!");
        }
        return textResult.getValue();
    }
}
